package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.constant.IsDeleteEnum;
import cn.com.cifi.mars.entity.PanoplanBuildPlan;
import cn.com.cifi.mars.entity.PanoplanBuildPlanItem;
import cn.com.cifi.mars.entity.PanoplanBuildPlanItemDetail;
import cn.com.cifi.mars.mapper.mars.PanoplanBuildPlanItemMapper;
import cn.com.cifi.mars.service.PanoplanBuildPlanItemService;
import cn.com.cifi.mars.util.UUIDUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 楼栋子表-组团楼栋 服务实现类
 * </p>
 *
 * @author yyd
 * @since 2019-09-07
 */
@Service
public class PanoplanBuildPlanItemServiceImpl extends ServiceImpl<PanoplanBuildPlanItemMapper, PanoplanBuildPlanItem> implements PanoplanBuildPlanItemService {

	private static final Logger log = LoggerFactory.getLogger(PanoplanBuildPlanItemServiceImpl.class);
	
    @Autowired
    private PanoplanBuildPlanItemMapper panoplanBuildPlanItemMapper;
    @Override
    public void syncPanoplanBuildPlanItem(JSONObject panoplanBuildPlanItem,String pkId,String itemId) {

        try {
            if (null == panoplanBuildPlanItem || panoplanBuildPlanItem.isEmpty() || null == pkId || null == itemId){
                throw new MsgException(CodeEnum.REQUEST_PARAM_NOT_FULL_ERROR);
            }else{
                PanoplanBuildPlanItem newPanoplanBuildPlanItem = new PanoplanBuildPlanItem();

                newPanoplanBuildPlanItem.setId(itemId);
                newPanoplanBuildPlanItem.setPkId(pkId);
                newPanoplanBuildPlanItem.setBatchId((String)panoplanBuildPlanItem.get("batchID"));
                newPanoplanBuildPlanItem.setBatchCode((String)panoplanBuildPlanItem.get("batchCode"));
                newPanoplanBuildPlanItem.setBatchName((String)panoplanBuildPlanItem.get("batchName"));
                newPanoplanBuildPlanItem.setBatchNum((String)panoplanBuildPlanItem.get("batchNum"));
                Object batchRoomNum = panoplanBuildPlanItem.get("batchRoomNum");
                if (null != batchRoomNum){
                    newPanoplanBuildPlanItem.setBatchRoomNum(Integer.parseInt(batchRoomNum.toString()) );
                }
                Object batchSaleArea = panoplanBuildPlanItem.get("batchSaleArea");
                if (null != batchSaleArea){
                    newPanoplanBuildPlanItem.setBatchSaleArea(new BigDecimal(batchSaleArea.toString()));
                }

                newPanoplanBuildPlanItem.setBldPrdId((String)panoplanBuildPlanItem.get("bldPrdId"));
                newPanoplanBuildPlanItem.setDecorateName((String)panoplanBuildPlanItem.get("decorateName"));
                newPanoplanBuildPlanItem.setFormatBuildingCode((String) panoplanBuildPlanItem.get("formatBuildingCode"));
                newPanoplanBuildPlanItem.setFormatBuildingName((String) panoplanBuildPlanItem.get("formatBuildingName"));
                newPanoplanBuildPlanItem.setProductTypeName((String) panoplanBuildPlanItem.get("productTypeName"));
                newPanoplanBuildPlanItem.setProductTypeNum((String) panoplanBuildPlanItem.get("productTypeNum"));
                Object salePark = panoplanBuildPlanItem.get("salePark");
                if (null != salePark){
                    newPanoplanBuildPlanItem.setSalePark(Integer.parseInt(salePark.toString()));
                }
                if (null != panoplanBuildPlanItem.get("totalRoomNum")){
                    newPanoplanBuildPlanItem.setTotalRoomNum(Integer.parseInt(panoplanBuildPlanItem.get("totalRoomNum").toString()));
                }
                newPanoplanBuildPlanItem.setSaleType((String) panoplanBuildPlanItem.get("saleType"));
                Object totalBuildArea = panoplanBuildPlanItem.get("totalBuildArea");
                if (null != totalBuildArea){
                    newPanoplanBuildPlanItem.setTotalBuildArea(new BigDecimal(totalBuildArea.toString()));
                }
                if (null != panoplanBuildPlanItem.get("batchBuildArea")){
                    newPanoplanBuildPlanItem.setBatchBuildArea(new BigDecimal(panoplanBuildPlanItem.get("batchBuildArea").toString()));
                }
                Object totalSaleArea = panoplanBuildPlanItem.get("totalSaleArea");
                if (null != totalSaleArea){
                    newPanoplanBuildPlanItem.setTotalSaleArea(new BigDecimal(totalSaleArea.toString()));
                }
                newPanoplanBuildPlanItem.setUniqueCode((String) panoplanBuildPlanItem.get("uniqueCode"));
                newPanoplanBuildPlanItem.setIsDeleted(IsDeleteEnum.NO.getKey());
                panoplanBuildPlanItemMapper.syncPanoplanBuildPlanItem(newPanoplanBuildPlanItem);
            }
        } catch (Exception e) {
            throw new MsgException(CodeEnum.REQUEST_PARAM_NOT_FULL_ERROR);
        }
    }

    @Override
    @Transactional
    public void syncAllDataItem(Map<String, List<JSONObject>> groupDetailMap) {
        try {
            if (!groupDetailMap.isEmpty()){

                String pkId = null;
                List<JSONObject> groupDetailItemList = null;
                Iterator<Map.Entry<String, List<JSONObject>>> iterator = groupDetailMap.entrySet().iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, List<JSONObject>> entry = iterator.next();
                    pkId = entry.getKey();
                    groupDetailItemList = entry.getValue();
                }
                List<PanoplanBuildPlanItem> entityList = new ArrayList<>();
                for (JSONObject groupDetailItemJsonObject : groupDetailItemList) {
                    PanoplanBuildPlanItem panoplanBuildPlanItem = new PanoplanBuildPlanItem();
                    panoplanBuildPlanItem.setId((String) groupDetailItemJsonObject.get("itemId"));
                    panoplanBuildPlanItem.setPkId(pkId);
                    panoplanBuildPlanItem.setBatchId((String) groupDetailItemJsonObject.get("batchID"));
                    panoplanBuildPlanItem.setBatchCode((String) groupDetailItemJsonObject.get("batchCode"));
                    panoplanBuildPlanItem.setBatchName((String) groupDetailItemJsonObject.get("batchName"));
                    panoplanBuildPlanItem.setBatchNum((String) groupDetailItemJsonObject.get("batchNum"));
                    panoplanBuildPlanItem.setProductTypeNum((String) groupDetailItemJsonObject.get("productTypeNum"));
                    panoplanBuildPlanItem.setBldPrdId((String) groupDetailItemJsonObject.get("bldPrdId"));
                    panoplanBuildPlanItem.setDecorateName((String) groupDetailItemJsonObject.get("decorateName"));
                    panoplanBuildPlanItem.setProTypeCode((String) groupDetailItemJsonObject.get("proTypeCode"));
                    panoplanBuildPlanItem.setProType((String) groupDetailItemJsonObject.get("proType"));
                    panoplanBuildPlanItem.setDecorateNum((String) groupDetailItemJsonObject.get("decorateNum"));
                    panoplanBuildPlanItem.setSaleNum((String) groupDetailItemJsonObject.get("saleNum"));                   
                    panoplanBuildPlanItem.setProductTypeName((String) groupDetailItemJsonObject.get("productTypeName"));
                    panoplanBuildPlanItem.setFormatBuildingCode((String) groupDetailItemJsonObject.get("formatBuildingCode"));
                    panoplanBuildPlanItem.setFormatBuildingName((String) groupDetailItemJsonObject.get("formatBuildingName"));
                    panoplanBuildPlanItem.setSaleType((String) groupDetailItemJsonObject.get("saleType"));
                    if (null != groupDetailItemJsonObject.get("totalRoomNum")){
                        panoplanBuildPlanItem.setTotalRoomNum(Integer.parseInt(groupDetailItemJsonObject.get("totalRoomNum").toString()));
                    }
                    if (null != groupDetailItemJsonObject.get("totalSaleArea")){
                        panoplanBuildPlanItem.setTotalSaleArea(new BigDecimal(groupDetailItemJsonObject.get("totalSaleArea").toString()));
                    }
                    if (null != groupDetailItemJsonObject.get("batchRoomNum")){
                        panoplanBuildPlanItem.setBatchRoomNum(Integer.parseInt(groupDetailItemJsonObject.get("batchRoomNum").toString()));
                    }
                    if (null != groupDetailItemJsonObject.get("batchSaleArea")){
                        panoplanBuildPlanItem.setBatchSaleArea(new BigDecimal(groupDetailItemJsonObject.get("batchSaleArea").toString()));
                    }
                    if (null != groupDetailItemJsonObject.get("salePark")){
                        panoplanBuildPlanItem.setSalePark(Integer.parseInt(groupDetailItemJsonObject.get("salePark").toString()));
                    }
                    if (null != groupDetailItemJsonObject.get("totalBuildArea")){
                        panoplanBuildPlanItem.setTotalBuildArea(new BigDecimal(groupDetailItemJsonObject.get("totalBuildArea").toString()));
                    }
                    if (null != groupDetailItemJsonObject.get("batchBuildArea")){
                        panoplanBuildPlanItem.setBatchBuildArea(new BigDecimal(groupDetailItemJsonObject.get("batchBuildArea").toString()));
                    }
                    panoplanBuildPlanItem.setUniqueCode((String) groupDetailItemJsonObject.get("uniqueCode"));
                    entityList.add(panoplanBuildPlanItem);
                }
                panoplanBuildPlanItemMapper.syncAllDataItem(entityList);
            }else{
                throw new MsgException(CodeEnum.REQUEST_PARAM_NOT_FULL_ERROR);
            }
        } catch (Exception e) {
            log.error("PanoplanBuildPlanItemServiceImpl.syncAllDataItem-->"+e);
        }
    }
}
